package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Attn;
import com.tianqu.tms.entity.Member;

public class CheckAccessCardNoAjaxAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CheckAccessCardNoAjaxAction.class);
	private static final String FROMTYPE = "fromType";
	private static final String VENDORID = "vendorId";

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute was called.");
		String accessCardNo = request.getParameter("accessCardNo");
		TxController controller = TxController.getTxController();
		controller.begin();
		//
		Member member = new Member();
		Attn attn = new Attn();
		member = member.getIntegralManagementInfo(accessCardNo);
		attn = attn.getIntegralInfo(accessCardNo);
		String r = "";
		if(member==null && attn==null){
			r = "【IM010】输入的积分卡不存在。|false|0";
		}else{
			if (member != null) {
				if (member.getUserId().equals("1")) {
					r = member.getRealName()+"|true|"+member.getVirtualMargin();
				} else if (member.getUserId().equals("2")) {
                    r = "【U1041】此会员卡被禁用，无法支付。|false|0";
				}else if(!StringUtil.isNullOrBlank(member.getVirtualMargin()) && Integer.parseInt(member.getVirtualMargin()) <= 0){
					 r = "【U1042】卡内过户费余额为零，无法支付。|false|0";
				}
			}else if(attn != null){
				if (attn.getUserId().equals("1")) {
					r = attn.getAttnName()+"|true|"+attn.getVirtualMargin();
				} else if (attn.getUserId().equals("2")) {
					 r = "【U1041】此会员卡被禁用，无法支付。|false|0";
				}else if(attn.getVirtualMargin() <= 0){
					 r = "【U1042】卡内过户费余额为零，无法支付。|false|0";
				}
			}
		}

		controller.commit();
		
		String log = "CheckAccessCardNo,accessCardNo:"+accessCardNo +" response:"+response;
		logger.debug(log);
		responseValues(response, r, TEXT);

		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}

}
